package com.ply.flashsalessystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ply.flashsalessystem.annotation.AuthorityName;
import com.ply.flashsalessystem.annotation.UserAuthority;
import com.ply.flashsalessystem.entity.OrderRecord;
import com.ply.flashsalessystem.entity.pojo.OrderForGoods;
import com.ply.flashsalessystem.entity.result.Result;
import com.ply.flashsalessystem.entity.status.OrderStatus;
import com.ply.flashsalessystem.entity.wrapper.WrapperOrder;
import com.ply.flashsalessystem.exception.OrderException;
import com.ply.flashsalessystem.service.OrderForGoodsService;
import com.ply.flashsalessystem.service.OrderRecordService;
import com.ply.flashsalessystem.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XiaoPan
 * @since 2022-03-28
 */
@RestController
@RequestMapping("/orderRecord")
@Api("订单记录 接口")
@CrossOrigin
public class OrderRecordController {
    @Autowired
    OrderRecordService orderRecordService;

    @Autowired
    OrderForGoodsService orderForGoodsService;


    //    @AuthorityName(authorityId = 26, authorityName = "用户 查询 订单状态 某个订单")
    @UserAuthority
    @ApiOperation("用户 查询 订单状态 某个订单 ")
    @GetMapping("/user/getOrderRecord/{ofgId}")
    public Result getOrderRecord(@PathVariable("ofgId") Integer ofgId) {
        //判断 该订单是否是 用户的
        OrderForGoods byId = orderForGoodsService.getById(ofgId);

        if (byId == null){
            throw new OrderException("订单id 错误");
        }

        if (byId.getUserId() != UserUtils.getUserId()){
            throw new OrderException("订单id 错误");
        }

        QueryWrapper<OrderRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ofg_id", ofgId).orderByDesc("create_time");
        List<OrderRecord> list = orderRecordService.list(queryWrapper);
        //todo 状态没有转为String
        return Result.ok().message("查询用户订单成功").data("rows", list);
    }

    @AuthorityName(authorityId = 27, authorityName = "管理员 条件查询订单状态")
    @ApiOperation("管理员 条件 查询 订单状态")
    @GetMapping(value = {"/admin/getOrderRecord/{ofgId}", "/admin/getOrderRecord"})
    public Result getOrderRecordAll(@PathVariable(value = "ofgId", required = false) Integer ofgId) {
        QueryWrapper<OrderRecord> queryWrapper = new QueryWrapper<>();
        if (ofgId != null) {
            queryWrapper.eq("ofg_id", ofgId).orderByDesc("create_time");
        }
        List<OrderRecord> list = orderRecordService.list(queryWrapper);

//todo 状态没有转为String
        return Result.ok().data("rows", list);
    }

}

